//
//  Array+SJAdd.swift
//  SJSwiftKit
//
//  Created by swordjoy on 2017/3/7.
//  Copyright © 2017年 swordjoy. All rights reserved.
//

import Foundation


// ~
postfix operator ~
public postfix func ~ (value: Int) -> SJSafeInt {
    return SJSafeInt(value: value)
}
// Struct
public struct SJSafeInt {
    var index: Int
    init(value: Int) {
        self.index = value
    }
}

public extension Array {
  
    /// 移除某个元素
    ///
    /// - Parameter object: 需要移除的元素
    public mutating func removeObject(_ object: Element) {
        var array: [String] = []
        for i in self {
              array.append("\(i)")
        }
        let index = array.index(of: "\(object)")
        if index != nil {
              self.remove(at: index!)
        }
    }

    public func circleObject(at index: Int) -> Element {
        return self[self.superCircle(at: index, size: self.count)]
    }

    private func superCircle(at index: Int, size maxSize: Int) -> Int {
        var _index = index
        if _index < 0 {
            _index = _index % maxSize
            _index += maxSize
        }
        if _index >= maxSize {
            _index = _index % maxSize
        }

        return _index
    }
    public subscript (safe index: Int) -> Element? {
         return (0..<count).contains(index) ? self[index] : nil
    }
    public subscript (index: SJSafeInt) -> Element? {
        get {
            return (self.startIndex..<self.endIndex).contains(index.index) ? self[index.index] : nil
        }
        set {
            if let newValue = newValue {
                if (self.startIndex..<self.endIndex).contains(index.index) {
                    self[index.index] = newValue
                }
            }
        }
    }
}
